<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:vb="http://www.cnsx.net/vb"
    extension-element-prefixes="msxsl"
	  exclude-result-prefixes="vb msxsl">

  <msxsl:script language="VB" implements-prefix="vb">
    <![CDATA[
    Dim FriendsCount as Integer = 0
    Dim OthersCount as Integer = 0
    Dim MainsCount as Integer = 0
    function Encode(byval str as string) as string
      Dim ret as string = ""
      try
        If str IsNot Nothing And str <> "" Then
          ret = replace(ret, "\", "\\")
          ret = replace(str, """", "\""")
          ret = replace(ret, "/", "\/")
          ret = replace(ret, vbCrLf, "\n")
          ret = replace(ret, vbCr, "\n")
          ret = replace(ret, vbLf, "\n")
          ret = replace(ret, vbTab, "\t")
          'ret = "hop"
        End If
      catch
        end try
		  return ret
	  end function
    function HasRoomForFriends() as Boolean
      FriendsCount += 1
      return CBool(FriendsCount <= 15)
    end function
    function HasRoomForOthers() as Boolean
      OthersCount += 1
      return CBool(OthersCount <= 8)
    end function
    function HasRoomForMains() as Boolean
      MainsCount += 1
      return CBool(MainsCount <= 2)
    end function
    ]]>
  </msxsl:script>
  
<xsl:template match="/">ghismolinks_loaded({"feeds":[<xsl:for-each select="/feeds/feed[not(tag/@value='main blogs')]"><xsl:choose><xsl:when test="tag[@value='sites amis']"><xsl:if test="vb:HasRoomForFriends()"><xsl:if test="position()>1">,</xsl:if><xsl:call-template name="tplCopyFeed"/></xsl:if></xsl:when><xsl:otherwise><xsl:if test="vb:HasRoomForOthers()"><xsl:if test="position()>1">,</xsl:if><xsl:call-template name="tplCopyFeed"/></xsl:if></xsl:otherwise></xsl:choose></xsl:for-each><xsl:for-each select="/feeds/feed[tag/@value='main blogs']"><xsl:if test="vb:HasRoomForMains()">,<xsl:call-template name="tplCopyFeed"/></xsl:if></xsl:for-each>]});</xsl:template>

<xsl:template name="tplCopyFeed">{"name":"<xsl:value-of select="vb:Encode(@name)"/>","siteurl":"<xsl:value-of select="vb:Encode(@siteurl)"/>","li_title":"<xsl:value-of select="vb:Encode(@li_title)"/>"}</xsl:template>

</xsl:stylesheet> 

